import 'dart:async'; // event bus需要
import 'package:flutter/material.dart';
import 'package:hello/events/demo_event.dart';

class DataSharingPage extends StatefulWidget {
  DataSharingPage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _DataSharingPageState createState() => _DataSharingPageState();
}

class _DataSharingPageState extends State<DataSharingPage> {

  StreamSubscription demoSubscription;
  num _count = 0;

  @override
  void initState(){
    super.initState();
    demoSubscription = eventBus.on<DemoEvent>().listen((event) {
      //缓存主题色
      setState(() {
        _count = event.count;
      });
    });
  }

  @override
  void dispose() {
    super.dispose();
    //取消订阅
    demoSubscription.cancel();
  }

  void navigateNamed(String name){
    Navigator.pushNamed(context, name);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('count = $_count'),
            FlatButton(
                child: Text("EventBus方式共享数据"),
                textColor: Colors.brown,
                onPressed: (){
                  navigateNamed('data_sharing_sub1');
                }),
            
          ],
        ),
      ),
    );
  }
}


